
package com.example.ecity.fragment;

import com.example.ecity.MapActivity;
import com.example.ecity.R;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;




public class ListFragment extends Fragment {

	private MapActivity mMapActivity;
	private ListView listing;
	ListAdapter adapter;
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_listpage, null);
		mMapActivity = (MapActivity)getActivity();
		listing  = (ListView) view.findViewById(R.id.listing);
		adapter = new ListAdapter(mMapActivity);
		listing.setAdapter(adapter);
		return view;
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	private class ListAdapter extends BaseAdapter {

		private Context mContext;
		private LayoutInflater inflater;

		public ListAdapter(Context context) {
			this.mContext = context;
			this.inflater = LayoutInflater.from(context);
			// this.inflater = (LayoutInflater)
			// mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub

			if(mMapActivity.msgRoute == null)
				return 0;
			return mMapActivity.msgRoute.getNumSteps();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mMapActivity.msgRoute.getStep(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView item = null;
			// if(convertView == null){
			convertView = inflater.inflate(R.layout.row, null);
			item = (TextView) convertView.findViewById(R.id.itemName);
			// }
			item.setText(mMapActivity.msgRoute.getStep(position).getContent());
			return convertView;
		}
	}
	public void refresh(){
		adapter.notifyDataSetChanged();
	}
}

